package com.hs.srm.tools;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.tools.FileObject;

import com.baomidou.mybatisplus.annotations.TableField;
import com.hs.srm.admin.modules.vendor.entity.VendorInfoEntity;

import sunnykid.character.UtilCharacter;
import sunnykid.file.FileException;
import sunnykid.file.FileOperator;
import sunnykid.jdbc.Column;
import sunnykid.jdbc.DataBaseManager;
import sunnykid.jdbc.DataBaseOperator;
import sunnykid.jdbc.JDBCException;
import sunnykid.text.SunnykidText;
import sunnykid.util.ClassUtils;

public class CodeGenTool {

	/**
	 * 生成html上的表格列代码
	 * @param tableName
	 * @throws JDBCException
	 */
	public static String writeGridCode(String tableName) throws JDBCException {
		String propPath = ClassUtils.getClassPath("/com/hs/srm/tools") + UtilCharacter.FILE_SEPARATOR
				+ "jdbc-mysql.properties";
		DataBaseOperator dbo = DataBaseManager.getOperatorInstance(propPath);
		Column[] columns = dbo.analyzeTable(tableName).getColumns();
		StringBuilder sb = new StringBuilder();

		sb.append("{checkbox: true},").append(UtilCharacter.WRAP);
		for(Column column : columns) {
			String columnName = column.getName();
			columnName = SunnykidText.underlineToCamel(columnName);
			columnName = SunnykidText.lowerFirstChar(columnName);

			sb.append("{title: '" + column.getComment() + "', field:'" + columnName + "'");
			if(column.isPrimaryKey()) {
				sb.append(",visible: false");
			}
			
			sb.append("},").append(UtilCharacter.WRAP);
		}
		return sb.deleteCharAt(sb.length() - 2).toString();
	}

	/**
	 * 生成Mapper上的resultMap代码
	 * @param entityCls
	 * @throws JDBCException
	 */
	public static String writeXMLCode(Class<?> entityCls) {
		String clsId = SunnykidText.lowerFirstChar(entityCls.getSimpleName());

		StringBuilder sb = new StringBuilder();
		sb.append("<!-- 可根据自己的需求，是否要使用 -->").append(UtilCharacter.WRAP);
		sb.append("<resultMap type=\"" + entityCls.getName() + "\" id=\"" + clsId + "\">").append(UtilCharacter.WRAP);

		Set<Field> fieldSet = ClassUtils.getAllFields(entityCls);
		sb.append("{checkbox: true},").append(UtilCharacter.WRAP);
		for(Field field : fieldSet) {
			TableField tableField = field.getAnnotation(TableField.class);
			if(tableField != null && tableField.value() != null && tableField.value().length() > 0) {
				String columnName = tableField.value();
				sb.append("<result property=\"" + field.getName() + "\" column=\"" + columnName + "\"/>").append(
						UtilCharacter.WRAP);
			}
		}
		sb.append("</resultMap>").append(UtilCharacter.WRAP);
		return sb.toString();
	}

	/**
	 * 根据VO类文件生成html中的代码
	 * @param voCls
	 * @return
	 * @throws FileException
	 */
	private static String writeHtmlCodeFromVO(File voFile) throws FileException {
		StringBuilder sb = new StringBuilder();
		List<String> list = FileOperator.readByLine(voFile);
		List<String> nList = new ArrayList<String>();
		for(String str : list) {
			nList.add(str.trim().replace(UtilCharacter.TAB, UtilCharacter.EMPTY));
		}
		
		int lineIdx = 0;
		sb.append("{checkbox: true},").append(UtilCharacter.WRAP);
		for(String str : nList) {
			if(str.startsWith("private ") && str.indexOf("static") < 0 && str.indexOf("final") < 0) {
				String[] sps = str.split(UtilCharacter.BLANK_SPACE);
				String fieldName = sps[sps.length - 1].replace(";", UtilCharacter.EMPTY).trim();
				
				String pre1 = nList.get(lineIdx - 1);
				String comment = null;
				if(pre1.startsWith("*")) {
					comment = nList.get(lineIdx - 2);
				} else if(pre1.startsWith("@")){
					comment = nList.get(lineIdx - 3);
				}
				if(comment != null) {
					comment = comment.replace("*", UtilCharacter.EMPTY).trim();
					sb.append("{title: '" + comment + "', field:'" + fieldName + "'},").append(UtilCharacter.WRAP);
				}
			}
			lineIdx++;
		}
		return sb.deleteCharAt(sb.length() - 2).toString();
	}

	public static void main(String[] args) {
		// try {
		// String code = writeGridCode("vendor_info");// 生成html中的grid代码
		// System.out.println(code);
		// } catch(Exception ex) {
		// ex.printStackTrace();
		// }

		// // 生成XML中的resultMap代码
		// String code = writeXMLCode(VendorInfoEntity.class);
		// System.out.println(code);

		try {
			String s = writeGridCode("cutpay_item");
			System.out.println(s);
		} catch(Exception ex) {
			ex.printStackTrace();
		}

	}

}
